package player;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;

import server.PlayerMessages;
import server.ServerMessages;

public class InputMessageManager extends Thread {

	private BufferedReader input;
	private PrintStream output;
	private Player myPlayer;

	
	
	private boolean end= false;
	
	// Constructor
	
	public InputMessageManager(BufferedReader input, Player myPlayer, PrintStream output) {
		this.input=input;
		this.output=output;
		this.myPlayer= myPlayer;
	}
	
	
	// Public method
	
	
	public void run() {
	
		do {
			
			String m=null;
			try { m = this.input.readLine();} catch (IOException e1) { e1.printStackTrace(); }
			
			// state: 'Disconnected'

			switch (myPlayer.state) {
			
			
				case Disconnected:
			
					this.myPlayer.state = PlayerState.WaitingPlayerID;
					break;

					
			///////////////////////////			
			// state: 'WaitingPlayerID'
			///////////////////////////		
					
				case WaitingPlayerID:
							
					this.myPlayer.readingPlayerId(m);
					this.myPlayer.state = PlayerState.StartingTheGame;
					break;

					
			///////////////////////////
			// state: 'startingTheGame'
			///////////////////////////	
					
				case StartingTheGame:

					try {
						this.output.println(PlayerMessages.ACCEPTED);
						m = this.input.readLine();
						System.out.println("Cards:"+m);
						myPlayer.startTheGame(m);
						this.myPlayer.state= PlayerState.SelectThreeCards;
					} catch (IOException e) { e.printStackTrace(); }
					break;

					
			////////////////////////////
			// state: 'SelectThreeCards'
			////////////////////////////
						
				case SelectThreeCards:
					try {
						String entryMsg = this.input.readLine();
						this.output.println("Accepted");
						
						this.myPlayer.receiveThreeCardsFromServer(entryMsg);
						this.myPlayer.state= PlayerState.Round;
							
					} catch (IOException e) { e.printStackTrace(); }
					break;
 

			////////////////////////////
			// state: 'Round'
			////////////////////////////
						
				case Round: 
						
					if (m.equals(ServerMessages.PLAY)) {
						// the player get the order to play!
						
						this.myPlayer.play();
						this.output.println(PlayerMessages.ACCEPTED);
						
					} else {
						// the player receives a card player by other!
						try {
							String entryMsg;
							if (m.equals(PlayerMessages.ACCEPTED)) {
								entryMsg = this.input.readLine();
								// The card is received at the beginning
							} else { entryMsg=m;}
								// The card is received is from the second player or later
							 
							this.output.println(PlayerMessages.ACCEPTED);
							this.myPlayer.receiveOneCardFromServer(entryMsg);
					
						} catch (IOException e) { e.printStackTrace(); }
					}
					break;
					
					
			////////////////////////////
			// state: 'EndOfRound'
			////////////////////////////
				
				case EndOfRound:
				
					this.myPlayer.state = PlayerState.Round;
					break;
				 
					
				
			////////////////////////////
			// state: 'EndOfHand'
			////////////////////////////
				
				case EndOfHand:
					this.myPlayer.state = PlayerState.StartingTheGame;
					break;
					 
					
					
			////////////////////////////
			// state: 'GameOver'
			////////////////////////////
				
				case GameOver:
					this.myPlayer.state = PlayerState.WaitingContinuation;
					this.myPlayer.sendContinue();
					break;

							
			////////////////////////////
			// state: 'WaitingContinuation'
			////////////////////////////
				
				case WaitingContinuation:
					this.myPlayer.state = PlayerState.WaitingPlayerID;
					this.myPlayer.decideContinue();
					break;					
					

					
				default:
					break;
			}

			
		} while(!this.end);
		
		
	}
	
	public void end() {
		this.end=true;
	}
	
}
